
#include "SceneEditor.h"
#include "SceneImageSaver.h"
#include "QDictionary.h"
#include "QArray.h"
#include "ImageInfo.h"


SceneImageSaver::SceneImageSaver(const QImageInfoList &bkgndList, const QImageInfoList &maskList,
                                 const QString &filePath)
    : mBkgndList(bkgndList)
    , mMaskList(maskList)
    , mFilePath(filePath)
{

}

SceneImageSaver::~SceneImageSaver()
{

}

void SceneImageSaver::run()
{
    emit started();

    if (mBkgndList.isEmpty() && mMaskList.isEmpty())
    {
        emit finished(0, tr("Save image finished !"));
        return;
    }

    QDictionary dict;

    int numberOfBkgnd = mBkgndList.size();
    int numberOfMask = mMaskList.size();
    int numberOfImages = numberOfBkgnd + numberOfMask;

    emit totalStep(numberOfImages);

    // 写图片数
    dict.insert(SCN_IMG_COUNT_KEY, numberOfImages);
    dict.insert(SCN_IMG_BKGND_COUNT_KEY, numberOfBkgnd);
    dict.insert(SCN_IMG_MASK_COUNT_KEY, numberOfMask);

    int step = 0;

    // 写背景图
    QImageInfoList::const_iterator itr = mBkgndList.constBegin();
    QArray images;

    while (itr != mBkgndList.constEnd())
    {
        ImageInfo *info = *itr;
        QDictionary imageInfo;
        const QString &name = info->name();
        imageInfo.insert(SCN_IMG_NAME_KEY, name);
        QPoint pos = info->position();
        imageInfo.insert(SCN_IMG_X_KEY, pos.x());
        imageInfo.insert(SCN_IMG_Y_KEY, pos.y());
        QVariant var;
        var.setValue(imageInfo);
        images.push_back(var);
        ++step;
        emit progress(step, name);
        ++itr;
    }

    images.display();

    QVariant v1;
    v1.setValue(images);
    dict.insert(SCN_IMG_BKGND_LIST_KEY, v1);

    // 写遮挡图
    images.clear();
    itr = mMaskList.constBegin();
    while (itr != mMaskList.constEnd())
    {
        ImageInfo *info = *itr;
        QDictionary imageInfo;
        const QString &name = info->name();
        imageInfo.insert(SCN_IMG_NAME_KEY, name);
        QPoint pos = info->position();
        imageInfo.insert(SCN_IMG_X_KEY, pos.x());
        imageInfo.insert(SCN_IMG_Y_KEY, pos.y());
        QVariant var;
        var.setValue(imageInfo);
        images.push_back(var);
        ++step;
        emit progress(step, name);
        ++itr;
    }

    images.display();

    QVariant v2;
    v2.setValue(images);
    dict.insert(SCN_IMG_MASK_LIST_KEY, v2);

    dict.display();

    // 写图像配置文件
    QFile file(mFilePath);
    if (!file.open(QIODevice::WriteOnly))
    {
        QString msg = QString(tr("Open file ")) + mFilePath + QString(tr(" failed !"));
        emit finished(-1, msg);
        return;
    }

    bool result = dict.archive(&file);
    if (!result)
    {
        file.close();
        QString msg = QString(tr("Write file ")) + mFilePath + QString(tr(" failed !"));
        emit finished(-1, msg);
        return;
    }

    file.close();

    emit finished(0, tr("Save image finished !"));
}
